//
//  DeptPicker.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/11/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "DeptPicker.h"
#import "HRStore.h"
#import "Dept.h"

@interface DeptPicker ()

@end

@implementation DeptPicker
@synthesize selectedDeptId, selectedDeptShortName, popView;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    dataSource = [[HRStore sharedStore] allDept];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DeptCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }
    
    Dept *d = (Dept *)[dataSource objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[d deptShortName]];
    [cell setIndentationLevel:[d deptId]];
    [cell setIndentationWidth:0.0];
    
    if ([d deptId] == selectedDeptId) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }
    
    
    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    selectedDeptId = (int)[cell indentationLevel];
    selectedDeptShortName = cell.textLabel.text;
    [[popView delegate] popoverControllerDidDismissPopover:popView];
}


@end
